package com.lanou.zakerproject.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;

import com.lanou.zakerproject.R;

/**
 * Created by dllo on 16/1/10.
 */
public class DayNightMode {
    public final static int THEME_SUN = 1;
    public final static int THEME_NIGHT = 2;

    public static void changeToTheme(Activity activity) {

        int theme1 = getDayNightMode(activity);
        int theme = (theme1 == THEME_SUN ? THEME_NIGHT : THEME_SUN);
        setDayNightMode(activity, theme);
        // 注意改过主题后一定要，把activity finish在重开一遍，因为更改主题只能在oncreate中进行
        activity.startActivity(new Intent(activity, activity.getClass()));
        activity.finish();
    }

    public static void onActivityCreateSetTheme(Activity activity) {
        int theme = getDayNightMode(activity);
        switch (theme) {
            case THEME_SUN:
                activity.setTheme(R.style.SunTheme);
                break;
            case THEME_NIGHT:
                activity.setTheme(R.style.NightTheme);
                break;
        }
    }

    private static SharedPreferences getSharedPreferences(Context context) {
        return context.getSharedPreferences("NightModeDemo", Context.MODE_PRIVATE);
    }

    public static int getDayNightMode(Context context) {
        SharedPreferences sharedPreferences = getSharedPreferences(context);
        return sharedPreferences.getInt("SUN_NIGHT_MODE", THEME_SUN);
    }

    public static void setDayNightMode(Context context, int mode) {
        SharedPreferences sharedPreferences = getSharedPreferences(context);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putInt("SUN_NIGHT_MODE", mode);
        editor.commit();
    }
}
